## Chapter 5 (Cross-Party Discussion) 
## Our Common Bonds 
## Winter 2021 - 2022  

## Read in source file 
source("ocb_replication_file.r")

## Read in the Data 
group <- read_csv(file="data/anonymized_data.csv") %>% 
  clean_names(.)

## Treatment assignment 
## 0 is control 
## 1 is polarization 
## 2 is moderation 
group <- group %>% 
  mutate(treatment = ifelse(article == "Beach",0,
                            ifelse(article == "Polarization",1,
                                   ifelse(article == "Consensus" | article == "Concensus",2,NA)))) 


## did the treatment improve FT ratings? 
group <- group %>% 
  mutate(other_ft = ifelse(party_id < 4,rep_ft,
                           ifelse(party_id >4,dem_ft, NA)))  

## trait ratings 
## combined trait ratings 
rev_hyp <- -1*group$other_hypocritical + 6 
rev_self <- -1*group$other_selfish + 6 
rev_mean <- -1*group$other_mean + 6 
combo_traits <- cbind(group$other_american,
                      group$other_intelligent,
                      group$other_honest,
                      group$other_generous,
                      group$other_open,
                      rev_hyp,
                      rev_self,
                      rev_mean) 
group$avg_trait <- apply(combo_traits,1,function(x){mean(x,na.rm=T)})

pdf(file="figures/chi_levendusky_fig05001.pdf")
group %>% 
  filter(treatment != 1) %>% 
  ggplot(data = .) + 
  geom_density(aes(x=other_ft, 
                   group=as.factor(treatment), 
                   linetype=as.factor(treatment),
                   fill=as.factor(treatment)), alpha=0.35) + 
  theme_bw() + 
  ylab("") +
  xlab("Out-Party Feeling Thermometer") + 
  ggtitle("") + 
  scale_fill_grey(name="", 
                  labels=c("Control","Heterogeneous Discussion"), 
                  start = 0.10, 
                  end = 0.8) + 
  scale_linetype(name="",
                 labels=c("Control","Heterogeneous Discussion"),
                 limits=c(0,2)) + 
  theme(plot.title = element_text(hjust=0.5),
        legend.position = "bottom",
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        panel.grid = element_blank()) 
dev.off() 

## Traits
pdf(file="figures/chi_levendusky_fig05002.pdf")
group %>% 
  filter(treatment != 1) %>% 
  filter(party_id != 4) %>% 
  filter(other_generous != 2.5) %>% 
  ggplot(data=.) + 
  geom_bar(aes(x=other_generous, 
               y=100*..prop..,
               fill=as.factor(treatment)),
           position="dodge") + 
  xlab("") + 
  ylab("Percentage")  + 
  scale_x_continuous(breaks=c(1:5),
                     labels=c("Not at All \n Well",
                              "Not Too \n Well",
                              "Somewhat \n Well",
                              "Very \n Well",
                              "Extremely \n Well")) + 
  theme_bw() +   
  ggtitle("") + 
  scale_fill_grey(name="", 
                  labels=c("Control","Heterogeneous Discussion"), 
                  start = 0.10, 
                  end = 0.8) + 
  theme(plot.title = element_text(hjust=0.5),
        legend.position = "bottom",
        panel.grid = element_blank()) 
dev.off() 


## Trust in the Other Party 
group <- group %>% 
  mutate(trust_other = ifelse(party_id < 4,trust_rep,
                              ifelse(party_id >4,trust_dem, NA))) 

pdf(file="figures/chi_levendusky_fig05003.pdf")
group %>% 
  filter(treatment != 1) %>% 
  ggplot(data=.) + 
  geom_bar(aes(x=trust_other, 
               y=100*..prop..,
               fill=as.factor(treatment)),
           position="dodge") + 
  xlab("") + 
  ylab("Percentage")  + 
  scale_x_continuous(breaks=c(1:5),
                     labels=c("Almost Never","Once in a While",
                              "About Half the Time",
                              "Most of the Time","Almost Always")) + 
  theme_bw() +   
  ggtitle("") + 
  scale_fill_grey(name="", 
                  labels=c("Control","Heterogeneous Discussion"), 
                  start = 0.10, 
                  end = 0.8) + 
  theme(plot.title = element_text(hjust=0.5),
        legend.position = "bottom",
        panel.grid = element_blank()) 
dev.off()

## Distributions 
prop.table(table(group$trust_other[group$treatment == 0]))
prop.table(table(group$trust_other[group$treatment == 2]))
## control: 36%, 46%, 16%, 2%, 0 
## treatment: 21%, 43%, 28%, 8%, <1% 

## Social Distance Items 
## scale so that higher values mean more comfort 
group <- group %>% 
  mutate(rev_friend = (-1*comfort_friend) + 5,
         rev_neighbor = (-1*comfort_neighbor)+5,
         rev_discuss = (-1*comfort_discuss) + 5) %>% 
  rowwise(.) %>% 
  mutate(soc_dist = mean(c(rev_friend,
                           rev_neighbor,
                           rev_discuss,
                           comfort_marriage), na.rm=T)) %>% 
  ungroup(.)

## Marriage Item 
pdf(file="figures/chi_levendusky_fig05004.pdf")
group %>% 
  filter(treatment != 1) %>% 
  filter(party_id != 4) %>% 
  ggplot(data=.) + 
  geom_bar(aes(x=comfort_marriage, 
               y=100*..prop..,
               fill=as.factor(treatment)),
           position="dodge") + 
  xlab("") + 
  ylab("Percentage")  + 
  scale_x_continuous(breaks=c(1:4),
                     labels=c("Extremely \n Upset",
                              "Somewhat \n Upset",
                              "Not Too \n Upset",
                              "Not at All \n Upset")) + 
  theme_bw() +   
  ggtitle("") + 
  scale_fill_grey(name="", 
                  labels=c("Control","Heterogeneous Discussion"), 
                  start = 0.10, 
                  end = 0.8) + 
  theme(plot.title = element_text(hjust=0.5),
        legend.position = "bottom",
        panel.grid = element_blank()) 
dev.off() 

## Future Discussion 
pdf(file="figures/chi_levendusky_fig05005.pdf")
group %>% 
  filter(treatment != 1) %>% 
  filter(party_id != 4) %>% 
  filter(rev_discuss != 3.5) %>% 
  ggplot(data=.) + 
  geom_bar(aes(x=rev_discuss, 
               y=100*..prop..,
               fill=as.factor(treatment)),
           position="dodge") + 
  xlab("") + 
  ylab("Percentage")  + 
  scale_x_continuous(breaks=c(1:4),
                     labels=c("Not at All \n Comfortable",
                              "Not Too \n Comfortable",
                              "Somewhat \n Comfortable",
                              "Extremely \n Comfortable")) + 
  theme_bw() +   
  ggtitle("") + 
  scale_fill_grey(name="", 
                  labels=c("Control","Heterogeneous Discussion"), 
                  start = 0.10, 
                  end = 0.8) + 
  theme(plot.title = element_text(hjust=0.5),
        legend.position = "bottom",
        panel.grid = element_blank()) 
dev.off() 



## BUT: note that social distance here is not very high in the control condition 
prop.table(table(group$comfort_neighbor[group$treatment==0 & group$party_id !=4]))
prop.table(table(group$comfort_discuss[group$treatment==0 & group$party_id !=4]))
prop.table(table(group$comfort_friend[group$treatment==0 & group$party_id !=4]))
prop.table(table(group$comfort_marriage[group$treatment==0 & group$party_id !=4]))
## Here, 85% are extremely/somewhat comfortable having those from the other party as neighbors 
## 62% are extremely/somewhat comfortable having a conversation with them 
## 73% are extremely/somewhat comfortable being friends with them 
## 59% are "not too upset" or "not at all upset" if their child marries someone from the other party 
## part of why there's no decline from the consensus treatment is that social distance isn't very high here! 

## Understand the other party's perspective 
pdf(file="figures/chi_levendusky_fig05006.pdf")
group %>% 
  filter(treatment != 1) %>% 
  filter(party_id != 4) %>% 
  ggplot(data=.) + 
  geom_bar(aes(x=understand_other, 
               y=100*..prop..,
               fill=as.factor(treatment)),
           position="dodge") + 
  xlab("") + 
  ylab("Percentage")  + 
  scale_x_continuous(breaks=c(1:5),
                     labels=c("Strongly \n Disagree",
                              "Disagree",
                              "Neither Agree \n Nor Disagree",
                              "Agree",
                              "Strongly \n Agree")) + 
  #                   drop = F) + 
  theme_bw() +   
  ggtitle("") + 
  scale_fill_grey(name="", 
                  labels=c("Control","Heterogeneous Discussion"), 
                  start = 0.10, 
                  end = 0.8) + 
  theme(plot.title = element_text(hjust=0.5),
        legend.position = "bottom",
        panel.grid = element_blank()) 
dev.off() 

## Think the other side takes reasonable positions 
pdf(file="figures/chi_levendusky_fig05007.pdf") 
group %>% 
  ## get rid of one person who chose two categories 
  filter(reasonable_position != 2.5) %>% 
  filter(treatment != 1) %>% 
  filter(party_id != 4) %>% 
  ggplot(data=.) + 
  geom_bar(aes(x=reasonable_position, 
               y=100*..prop..,
               fill=as.factor(treatment)),
           position="dodge") + 
  xlab("") + 
  ylab("Percentage")  + 
  scale_x_continuous(breaks=c(1:5),
                     labels=c("Strongly \n Disagree",
                              "Disagree",
                              "Neither Agree \n Nor Disagree",
                              "Agree",
                              "Strongly \n Agree")) + 
  theme_bw() +   
  ggtitle("") + 
  scale_fill_grey(name="", 
                  labels=c("Control","Heterogeneous Discussion"), 
                  start = 0.10, 
                  end = 0.8) + 
  theme(plot.title = element_text(hjust=0.5),
        legend.position = "bottom",
        panel.grid = element_blank()) 
dev.off() 

## Other Party Respects My Opinions  
pdf(file="figures/chi_levendusky_fig05008.pdf")
group %>% 
  filter(treatment != 1) %>% 
  ggplot(data=.) + 
  geom_bar(aes(x=respect_opinion, 
               y=100*..prop..,
               fill=as.factor(treatment)),
           position="dodge") + 
  xlab("") + 
  ylab("Percentage")  + 
  scale_x_continuous(breaks=c(1:5),
                     labels=c("Strongly Disagree","Disagree",
                              "Neither Agree \n Nor Disagree",
                              "Agree","Strongly Agree")) + 
  theme_bw() +   
  ggtitle("") + 
  scale_fill_grey(name="", 
                  labels=c("Control","Heterogeneous Discussion"), 
                  start = 0.10, 
                  end = 0.8) + 
  theme(plot.title = element_text(hjust=0.5),
        legend.position = "bottom",
        panel.grid = element_blank()) 
dev.off()

## Duration Effects? 
group <- group %>% 
  mutate(follow_other_ft = ifelse(party_id < 4, q7_2,
                                  ifelse(party_id > 4, q7_1,NA)),
         follow_same_ft = ifelse(party_id > 4, q7_2,
                                 ifelse(party_id < 4, q7_1,NA)),
         follow_ft_diff = follow_same_ft - follow_other_ft,
         follow_other_trust = ifelse(party_id > 4, q26,
                                     ifelse(party_id < 4, q27,NA)), 
         follow_rev_discuss = ifelse(!is.na(q18),(-1*q18)+5,
                                     ifelse(!is.na(q19),(-1*q19)+5,NA)),
         follow_rev_neighbor = ifelse(!is.na(q20),(-1*q20)+5,
                                      ifelse(!is.na(q21),(-1*q21)+5,NA)),
         follow_rev_friend = ifelse(!is.na(q22),(-1*q22)+5,
                                    ifelse(!is.na(q23),(-1*q23)+5,NA))) %>% 
  ## marriage item gets botched, drop it here (response categories get messed up)
  rowwise(.) %>% 
  mutate(follow_soc_dist = mean(c(follow_rev_discuss,
                                  follow_rev_friend,
                                  follow_rev_neighbor),na.rm=T)) %>%
  ungroup(.) 

## For standard errors: cluster by group 
group <- group %>% 
  mutate(cluster_group = paste(session_number,group_number,sep="-"))

## Regression models 
mf1 <- lm_robust(follow_other_ft ~ as.factor(treatment) + as.factor(session_number),
                 subset = treatment !=1,
                 cluster = cluster_group, 
                 data = group) 
mf2 <- lm_robust(follow_other_trust ~ as.factor(treatment)+ as.factor(session_number),
                 subset = treatment !=1,
                 cluster = cluster_group, 
                 data = group) 
mf3 <- lm_robust(follow_soc_dist ~ as.factor(treatment)+ as.factor(session_number),
                 subset = (treatment !=1 & party_id !=4),
                 cluster = cluster_group, 
                 data = group) 

## Regular models (needed for stargazer)
mf1a <- lm(follow_other_ft ~ as.factor(treatment) + as.factor(session_number),
           subset = treatment !=1,
           data = group) 
mf2a <- lm(follow_other_trust ~ as.factor(treatment) + as.factor(session_number),
           subset = treatment !=1,
           data = group) 
mf3a <- lm(follow_soc_dist ~ as.factor(treatment) + as.factor(session_number),
           subset = (treatment !=1 & party_id != 4),
           data = group) 


## Output to a table 
stargazer(mf1a,mf2a,mf3a,
          se = starprep(mf1,mf2,mf3),
          column.labels = c("Out-Party FT","Out-Party Trust",
                            "Social Distance"),
          covariate.labels = c("Heterogenenous Discussion"),
          digits = 2, 
          keep.stat = c("n","rsq"),
          type = "html",
          out = "tables/raw output/table_five_one.html")
